import {register, login, getUserInfo, UpdUserInfo} from '../api/user'
import {Toast} from "antd-mobile";

import {SetCookie,SetLocalStorage} from './LogicalOperation'


// 登录的同步action
export function $Login(user){
    return async (dispatch) => {
        let {Account,PassWord,Code} = user
        Account = Account.replace(/\s+/g,"")
        try{
            // 发送请求
            let response = await login({
                Account,PassWord,Code
            })
            let {data,status} = response
            if (data.code === 200) {
                Toast.success(data.msg);
                SetCookie('token',data.data.Token)
                dispatch({
                    type:"LoginOk",
                    data
                })
            } else {
                Toast.info(data.msg);
                dispatch({
                    type:"OtherError",
                    data
                })
            }
        }catch(e){
            dispatch({
                type:"NetWorkError",
                msg:e
            })
        }
    }
}
// 获取用户信息的同步action
export function $GetUserInfo(user){
    return async (dispatch) => {
        try{
            // 发送请求
            let response = await getUserInfo()
            let {data,status} = response
            if (data.code === 200) {
                Toast.success(data.msg);
                SetLocalStorage('UserInfo',data.data)
                dispatch({
                    type:"GetUserInfoOk",
                    data
                })
            } else {
                Toast.info(data.msg);
                dispatch({
                    type:"OtherError",
                    data
                })
            }
        }catch(e){
            dispatch({
                type:"NetWorkError",
                msg:e
            })
        }
    }
}
